//
//  Date+Extension.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/11.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import Foundation
extension Date{
    
    func dateStr() -> String {
        let formatter = DateFormatter.init()
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = "yyyy.MM.dd HH:mm:ss"
        let str = formatter.string(from: self)
        return str
    }
    
    func exactlyMonth()->String {
        let formatter = DateFormatter.init()
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = "MM月"
        let str = formatter.string(from: self)
        return str
    }
    
    static func recentThreeMonths() -> Array<String> {
        let date = Date.init(timeIntervalSinceNow: 0)
        let calendar = Calendar.init(identifier: .chinese)
        var components = DateComponents.init()
        components.month = -1
        
        if let foreMonth = calendar.date(byAdding: components, to: date) {
            if let foreforeMonth = calendar.date(byAdding: components, to: foreMonth) {
                return [date.exactlyMonth(),foreMonth.exactlyMonth(),foreforeMonth.exactlyMonth()].sorted()
            }
        }
        return ["","",""]
    }
    
}
